package com.leanote.berial.tingphone.util;

/**
 * Created with InetlliJ IDEA.
 * Project: com.liuhui.zhufengfm.app.util
 * user  Pinocchio
 * Date 2015/7/28
 * Email:liu594545591@126.com
 */

import java.io.*;
import java.net.HttpURLConnection;

/**
 * 用于读取IO流
 */
public class StreamUtil {
    private StreamUtil() {
    }
    public static void close(Object stream){
        if (stream != null) {
            try {
                if (stream instanceof InputStream) {
                    ((InputStream) stream).close();
                }else if (stream instanceof OutputStream){
                    ((OutputStream) stream).close();
                }else if(stream instanceof Reader){
                    ((Reader) stream).close();
                }else if (stream instanceof Writer){
                    ((Writer) stream).close();
                }else if(stream instanceof HttpURLConnection){
                    ((HttpURLConnection) stream).disconnect();
                }
            }catch (Exception e){

            }
        }
    }

    /**
     * 将输入流中的数据存储到自己的数组中
     * @param is
     * @return
     */
    public static byte[] readStream(InputStream is) throws IOException {
        byte [] ret = null;
        if (is != null) {
            byte [] buf = new byte[128];
            int len;
            ByteArrayOutputStream bas = new ByteArrayOutputStream();
            while ((len =is.read(buf))!=-1){
                bas.write(buf,0,len);
                bas.flush();
            }
            if (bas != null) {
                ret = bas.toByteArray();
            }
        }
        return ret;
    }
}
